package cn.csc.module.order.service.addr;

import cn.csc.framework.common.pojo.PageResult;
import cn.csc.module.order.controller.admin.addr.vo.OrderAddrCreateReqVO;
import cn.csc.module.order.controller.admin.addr.vo.OrderAddrExportReqVO;
import cn.csc.module.order.controller.admin.addr.vo.OrderAddrPageReqVO;
import cn.csc.module.order.controller.admin.addr.vo.OrderAddrUpdateReqVO;
import cn.csc.module.order.convert.addr.OrderAddrConvert;
import cn.csc.module.order.convert.item.OrderItemConvert;
import cn.csc.module.order.dal.dataobject.addr.OrderAddrDO;
import cn.csc.module.order.dal.mysql.addr.OrderAddrMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.math.BigInteger;
import java.util.Collection;
import java.util.List;

import static cn.csc.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.csc.module.order.enums.ErrorCodeConstants.ORDER_ADDR_NOT_EXISTS;

/**
 * 用户订单配送地址 Service 实现类
 *
 * @author liuchuang
 */
@Service
@Validated
public class OrderAddrServiceImpl implements OrderAddrService {

    @Resource
    private OrderAddrMapper addrMapper;

    @Override
    public BigInteger createAddr(OrderAddrCreateReqVO createReqVO) {
        // 插入
        OrderAddrDO addr = OrderAddrConvert.INSTANCE.convert(createReqVO);
        addrMapper.insert(addr);
        // 返回
        return addr.getId();
    }

    @Override
    public void updateAddr(OrderAddrUpdateReqVO updateReqVO) {
        // 校验存在
        validateAddrExists(updateReqVO.getId());
        // 更新
        OrderAddrDO updateObj = OrderAddrConvert.INSTANCE.convert(updateReqVO);
        addrMapper.updateById(updateObj);
    }

    @Override
    public void deleteAddr(BigInteger id) {
        // 校验存在
        validateAddrExists(id);
        // 删除
        addrMapper.deleteById(id);
    }

    private void validateAddrExists(BigInteger id) {
        if (addrMapper.selectById(id) == null) {
            throw exception(ORDER_ADDR_NOT_EXISTS);
        }
    }

    @Override
    public OrderAddrDO getAddr(BigInteger id) {
        return addrMapper.selectById(id);
    }

    @Override
    public List<OrderAddrDO> getAddrList(Collection<BigInteger> ids) {
        return addrMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<OrderAddrDO> getAddrPage(OrderAddrPageReqVO pageReqVO) {
        return addrMapper.selectPage(pageReqVO);
    }

    @Override
    public List<OrderAddrDO> getAddrList(OrderAddrExportReqVO exportReqVO) {
        return addrMapper.selectList(exportReqVO);
    }

    @Override
    public OrderAddrDO getAddrByOrderId(BigInteger orderId) {
        List<OrderAddrDO> list=addrMapper.selectList("order_id",orderId);
        return !list.isEmpty()?list.get(0):null;
    }

    @Override
    public OrderAddrDO getLogistics(BigInteger orderId, BigInteger orderItemId) {
        QueryWrapper<OrderAddrDO> q= new QueryWrapper<>();
        q.eq("order_id",orderId);
        q.eq("order_item_id",orderItemId);
        List<OrderAddrDO> list=addrMapper.selectList(q);
        return !list.isEmpty()?list.get(0):null;
    }

    @Override
    public void updateBatch(List<OrderAddrUpdateReqVO> updateReqVO) {
        for(OrderAddrUpdateReqVO up :updateReqVO ){

            UpdateWrapper<OrderAddrDO>  updateWrapper= new UpdateWrapper();
            updateWrapper.set("logistics_com",up.getLogisticsCom());
            updateWrapper.set("logistics_num",up.getLogisticsNum());
            updateWrapper.eq("order_item_id",up.getOrderItemId());
            updateWrapper.eq("order_id",up.getOrderId());
            addrMapper.update(new OrderAddrDO(),updateWrapper);
        }
    }

}
